/*
 * description: 网络请求封装
 */
import { baseUrl } from "@/utils/config";
import store from "@/store";

let btnLock = false;
function Request(options) {
  if (!options.data) {
    options.data = {};
  }
  options.data.openid = uni.getStorageSync("token");
  // let header = {
  //   Authorization: "Bearer " + uni.getStorageSync("token"),
  //   "tenant-id": 1,
  // };
  return new Promise((resolve, reject) => {
    if (!options.hideLoading) {
      uni.showLoading({
        title: "加载中...",
      });
    }
    uni.request({
      ...options,
      header: {
        r: Date.parse(new Date()),
        // ...header,
      },
      success: (res) => {
        const { code } = res.data;
        resolve(res.data);
        if (!options.hideLoading) {
          uni.hideLoading({
            success: () => {},
            fail: () => {},
            complete: () => {},
          });
        }
        switch (code) {
          case 200:
            resolve(res.data);
            break;
          case 401:
            if (!btnLock) {
              btnLock = true;
              uni.showToast({
                title: "登录信息已过期",
                icon: "none",
              });
              uni.removeStorageSync("token");
              store.dispatch("clearToken");
              uni.removeStorageSync("userInfo");
              store.dispatch("clearUserInfo");
              setTimeout(() => {
                uni.reLaunch({
                  url: "/pages/index/index",
                });
              }, 2000);
            }
            break;
          default:
            uni.showToast({
              icon: "none",
              title: res.data.msg,
            });
            reject(res.data);
        }
      },
    });
  });
}

// 服务地址变更
export function Service(value) {
  return function (target) {
    target.prototype.namespace = value || "";
  };
}

// 基础请求封装
export class BaseService {
  request(options = {}) {
    let path = "";
    if (process.env.NODE_ENV === "development") {
      path = this.namespace;
    } else {
      path = baseUrl + "/" + this.namespace;
    }
    options.url = path + options.url;
    options.method = options.method || "GET";

    if (options.method === "GET" || options.method === "get") {
      options.data = options.params;
    }
    return Request(options);
  }
  get(params) {
    return this.request({
      url: "/get",
      params: {
        ...params,
      },
    });
  }
  list(params) {
    return this.request({
      url: "/list",
      params: {
        ...params,
      },
    });
  }
  page(params) {
    return this.request({
      url: "/page",
      params: {
        ...params,
      },
    });
  }
  create(params) {
    return this.request({
      url: "/create",
      method: "POST",
      data: {
        ...params,
      },
    });
  }
  update(params) {
    return this.request({
      url: "/update",
      method: "POST",
      data: {
        ...params,
      },
    });
  }
  cancel(id) {
    return this.request({
      url: `/cancel?id=${id}`,
      method: "DELETE",
    });
  }
  info(id) {
    return this.request({
      url: `/${id}`,
      method: "GET",
    });
  }
}

export { Request };
